import pptx
from pptx.util import Inches, Pt
from pptx.enum.shapes import MSO_SHAPE
from pptx.dml.color import RGBColor
from pptx.chart.data import CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_LEGEND_POSITION
import utils

prs = pptx.Presentation()  # 创建pptx对象
slides = prs.slides  # 幻灯片列表
# 第一页
slide1 = slides.add_slide(prs.slide_layouts[0])  # 添加索引0的幻灯片模板
slide1.shapes.title.text = '4S汽车消费者洞察报告'
slide1.shapes.placeholders[1].text = '2020年第一季度'
# 第二页
slide2 = slides.add_slide(prs.slide_layouts[1])  # 添加索引1的幻灯片模板
slide2.shapes.title.text = '目录'
content = slide2.shapes.placeholders[1].text_frame
content.add_paragraph().text = '4S店第一季度汽车销售榜单'
content.add_paragraph().text = '汽车用户消费偏好趋势'
content.add_paragraph().text = '汽车消费者用户画像'
# 第三页
slide3 = slides.add_slide(prs.slide_layouts[5])  # 添加索引5的幻灯片模板
slide3.shapes.title.text = '4S店第一季度汽车销售榜单'
# 创建11行4列的表格;   距离左边:0  距离上边:1.4  单元格宽度:10  高度:5
table = slide3.shapes.add_table(11, 4, Inches(0), Inches(1.4), Inches(10), Inches(5)).table
# 写入内容
table.cell(0, 0).text = '排名'
table.cell(0, 1).text = '车型'
table.cell(0, 2).text = '所属厂商'
table.cell(0, 3).text = '一季度销售额'
data = [{'type': '日产轩逸', 'base': '东风日产', 'total': 50000},
        {'type': '大众宝来', 'base': '一起大众', 'total': 46410},
        {'type': '吉利帝豪', 'base': '吉利汽车', 'total': 43400},
        {'type': '日产轩逸', 'base': '东风日产', 'total': 50000},
        {'type': '日产轩逸', 'base': '东风日产', 'total': 50000},
        {'type': '日产轩逸', 'base': '东风日产', 'total': 50000},
        {'type': '日产轩逸', 'base': '东风日产', 'total': 200000},
        {'type': '日产轩逸', 'base': '东风日产', 'total': 150000},
        {'type': '日产轩逸', 'base': '东风标志', 'total': 150000},
        {'type': '小米', 'base': '国风SVU', 'total': 250000}
        ]
for i in range(len(data)):
    index = i+1
    table.cell(index, 0).text = str(index)
    table.cell(index, 1).text = data[index - 1]['type']
    table.cell(index, 2).text = data[index - 1]['base']
    table.cell(index, 3).text = str(data[index - 1]['total'])
# 第四页
slide4 = slides.add_slide(prs.slide_layouts[5])  # 添加索引5的幻灯片模板
slide4.shapes.title.text = '汽车用户消费偏好趋势'
# 三个矩形
shape1 = slide4.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(1), Inches(1.5), Inches(3), Inches(2.5))
fill = shape1.fill
fill.solid()
fill.fore_color.rgb = RGBColor(51, 102, 255)
shape1.text_frame.add_paragraph().text = '价位偏好:'
p1 = shape1.text_frame.add_paragraph()
p1.text = '经济入门型'
p1.font.size = Pt(35)

shape2 = slide4.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(4), Inches(1.5), Inches(5), Inches(2.5))
p2 = shape2.text_frame.add_paragraph()
p2.text = '国产自主品牌'
p2.font.size = Pt(35)

shape3 = slide4.shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(1), Inches(4), Inches(8), Inches(2.5))
p3 = shape3.text_frame.add_paragraph()
p3.text = 'SUV车型仍为主力'
p3.font.size = Pt(35)

# 第五页
slide5 = slides.add_slide(prs.slide_layouts[5])
slide5.shapes.title.text = '汽车消费者用户画像'
chart_data = CategoryChartData()
chart_data.categories = ['70后', '80后', '90后']
chart_data.add_series('各年龄段平均购车花费', (40, 45, 25))
slide5.shapes.add_chart(
    XL_CHART_TYPE.COLUMN_CLUSTERED, Inches(1), Inches(2.5), Inches(4), Inches(3), chart_data
)

chart_data1 = CategoryChartData()
chart_data1.categories = ['男', '女']
chart_data1.add_series('性别分布', (65, 35))
chart = slide5.shapes.add_chart(
    XL_CHART_TYPE.PIE, Inches(6), Inches(2.5), Inches(3), Inches(3), chart_data1
).chart

chart.has_legend = True
chart.legend.position = XL_LEGEND_POSITION.RIGHT

# 第六页
slide6 = slides.add_slide(prs.slide_layouts[5])
slide6.shapes.title.text = '谢谢!'

prs.save(utils.get_project_root_abs_path() + '/myFile/ppt/report.pptx')
